<?php

declare(strict_types=1);

namespace WebTus;

class TusFileHash
{
    public $id;
    public $hash;
    public $name;
    public $path;
    public $times = 1; //引用次数 为0时才能删除文件
    public $size;
    public $mtime;

    /**
     * TusFileHash constructor.
     * @param array|null $items
     */
    public function __construct(array $items = null)
    {
        if (is_array($items)) {
            foreach ($items as $item => $value) {
                $this->$item = $value;
            }
        }
    }

    public function __toString()
    {
        return TusService::json([
            'id' => $this->id,
            'name' => $this->name,
            'path' => $this->path,
            'times' => $this->times,
            'size' => $this->size,
            'mtime' => $this->mtime
        ]);
    }

    public function times(int $num = 1)
    {
        $this->times += $num;
        if ($this->times < 0) {
            $this->times = 0;
        }
    }
}
